package com.futures.investm.common;

import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.content.ContextCompat;
import android.text.TextUtils;
import android.view.WindowManager;

import com.futures.investm.MainActivity;
import com.futures.investm.R;
import com.futures.investm.utils.ApplicationUtil;
import com.futures.investm.utils.CacheData;
import com.aries.ui.util.StatusBarUtil;
import com.avos.avoscloud.AVException;
import com.avos.avoscloud.AVObject;
import com.avos.avoscloud.AVQuery;
import com.avos.avoscloud.AVUser;
import com.avos.avoscloud.FindCallback;
import com.avos.avoscloud.LogInCallback;

import java.util.List;

import tech.com.commoncore.base.BaseActivity;
import tech.com.commoncore.utils.DataUtils;
import tech.com.commoncore.utils.FastUtil;
import tech.com.commoncore.utils.ToastUtil;

public class SplashActivity extends BaseActivity {

    private Handler mHandler = new Handler();
    private String url;
    private String titleColor;

    @Override
    public void beforeSetContentView() {
        if (!isTaskRoot()) {//防止应用后台后点击桌面图标造成重启的假象---MIUI及Flyme上发现过(原生未发现)
            finish();
            return;
        }
        super.beforeSetContentView();
    }

    @Override
    public void beforeInitView() {
        super.beforeInitView();
    }


    @Override
    public int getContentLayout() {
        return R.layout.activity_splash;
    }

    @Override
    public void initView(Bundle savedInstanceState) {
        if (!isTaskRoot()) {
            return;
        }
        if (!StatusBarUtil.isSupportStatusBarFontChange()) {
            //隐藏状态栏
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
        }
        Drawable drawable = ContextCompat.getDrawable(mContext, R.mipmap.qidongye);
        FastUtil.getTintDrawable(drawable, Color.WHITE);
        mContentView.setBackgroundColor(Color.WHITE);

        AVQuery<AVObject> query = new AVQuery<>("CheckVersion");
        query.findInBackground(new FindCallback<AVObject>() {
            @Override
            public void done(List<AVObject> list, AVException e) {
                if (e == null) {
                    if (list != null && list.size() > 0) {
                        //获取渠道名
                        String channel = ApplicationUtil.getMetaValueFromApp(mContext,"UMENG_CHANNEL");

                        AVObject channelObject = list.get(0);

                        for (AVObject avObject : list) {
                            if(avObject.getString("channel").equals(channel)){
                                channelObject= avObject;
                                break;
                            }
                        }

//                        boolean isOpen = channelObject.getBoolean(BussConstant.IS_OPEN);

                        String versionCode = channelObject.getString("versionCode");



                        if (versionCode.equals("1")) {

                            url = channelObject.getString("versionUrl");

                            if (TextUtils.isEmpty(url)){

                                mHandler.postDelayed(runnable, 1000);

                            }else {

//                                titleColor = channelObject.getString("versionCol");

//                                mHandler.postDelayed(runnable2, 1000);


                                //去到webview页面.
                                startActivity(tech.com.commoncore.base.BrowerActivity.actionToActivity(mContext,channelObject.getString("versionUrl")
                                        ,channelObject.getString("versionBg")));
                                SplashActivity.this.finish();



                            }


                        } else {
                            //去正常页面
                            mHandler.postDelayed(runnable, 1000);
                        }
                    } else {
                        FastUtil.startActivity(mContext, ErrorActivity.class);
                        SplashActivity.this.finish();
                    }

                } else {
                    FastUtil.startActivity(mContext, ErrorActivity.class);
                    SplashActivity.this.finish();
                }

            }
        });

    }


    Runnable runnable2 = new Runnable() {
        @Override
        public void run() {
            if (!isFinishing()) {
                startActivity(BrowerActivity.actionToActivity(mContext, url, titleColor));
                SplashActivity.this.finish();
            }
        }
    };


    Runnable runnable = new Runnable() {
        @Override
        public void run() {
            if (!isFinishing()) {
                if (CacheData.isFirst(mContext)) {
                    //初次使用
                    FastUtil.startActivity(mContext, GuideActivity.class);
                    SplashActivity.this.finish();
                }
                else if (!CacheData.isLogin(mContext)) {
                    FastUtil.startActivity(mContext, MainActivity.class);
//                    FastUtil.startActivity(mContext, LoginActivity.class);
                    SplashActivity.this.finish();
                }
                else {
                    FastUtil.startActivity(mContext, MainActivity.class);
                    autoLogin();
                }
            }

        }
    };

    //自动登录
    private void autoLogin() {
        //获取缓存账户密码
        String account = CacheData.getLoginAccount(mContext);
        String password = CacheData.getLoginPassword(mContext);
        if (DataUtils.isEmptyString(account) || DataUtils.isEmptyString(password)) {
            //如果账户或密码为空
            CacheData.setLoginUserId(mContext, "");
            CacheData.setLoginUser(null);
            CacheData.setLoginAccount(mContext, "");
            CacheData.setLoginPassword(mContext, "");
            SplashActivity.this.finish();
        } else {
            login(account, password);
        }
    }

    //登录
    private void login(final String account, final String password) {
        AVUser.loginByMobilePhoneNumberInBackground(account, password, new LogInCallback<AVUser>() {
            @Override
            public void done(AVUser avUser, AVException e) {
                if (null == e) {
                    //登录成功
                    //缓存登录信息
                    CacheData.setLoginUserId(mContext, avUser.getObjectId());
                    CacheData.setLoginUser(avUser);
                    CacheData.initLoginAccount(mContext, account, password);
//                    缓存用户自选信息
//                    CacheData.setOptionalList(avUser.getList(BussConstant.OPTION_FUTURE));
                    FastUtil.startActivity(SplashActivity.this, MainActivity.class);
                    SplashActivity.this.finish();
                } else {
                    //登录失败
                    ToastUtil.show("登录失败,请重新登录");
                    CacheData.setLoginUserId(mContext, "");
                    CacheData.setLoginUser(null);
                    CacheData.setLoginPassword(mContext, "");
//                    FastUtil.startActivity(mContext, LoginRegistActivity.class);
                    FastUtil.startActivity(mContext, LoginActivity.class);
                    SplashActivity.this.finish();
                }
            }
        });
    }
}
